package com.bijava.entity;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.util.Properties;

/**
 * @author ljj
 * @date 2019/7/19
 */
public class GencodePath {
    public static String rootDir;
    public static String tmpDir;

    public static String zipDir;

    public static String apiFtlDir;

    public static String frameworkFtlDir;

    public static String ftlUploadDir;

    static {
        YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
        yamlFactory.setResources(new ClassPathResource("application.yml"));
        Properties properties =  yamlFactory.getObject();
        String runInServer = properties.getProperty("run_in_server","false");
        tmpDir = properties.getProperty("server.tomcat.basedir","/tmp");
        zipDir = tmpDir + "/zip";
        File zipFile = new File(zipDir);
        if(!zipFile.exists()){
            zipFile.mkdirs();
        }
        if(Boolean.valueOf(runInServer)){
            rootDir = "/project-template";
            apiFtlDir = tmpDir + rootDir + "/apiftl";
            frameworkFtlDir = tmpDir +rootDir + "/frameworkftl";
            ftlUploadDir = tmpDir + rootDir + "/ftl";
        }else {
            rootDir = GencodePath.class.getClassLoader().getResource("project-template").getPath();
            apiFtlDir = rootDir + "/apiftl";
            frameworkFtlDir = rootDir + "/frameworkftl";
            ftlUploadDir = tmpDir + rootDir + "/ftl";
        }
    }
}
